package itdep.jsf.internal;

import itdep.domain.IisEntity;
import itdep.services.IisEntityService;
import org.richfaces.model.Ordering;

/**
 * Created: 17.04.2009 13:04:26 by Tolar
 */
public class CommonTableBean<E extends IisEntity> extends BaseTableBean<E> {
	private IisEntityService<E> service;
	private Ordering ordering = Ordering.ASCENDING;

	public IisEntityService<E> getService() {
		return service;
	}

	public void setService(final IisEntityService<E> service) {
		this.service = service;
		setDataProvider(new EntityDataProvider<Long, E>(service));
	}

	@Override
	protected E addMethod() {
		return service.create();
	}

	@Override
	protected E saveMethod(final E record) {
		if (record != null && record.isNew()) {
			service.save(record);
		} else {
			service.update(record);
		}
		return null;
	}

	@Override
	protected E deleteMethod(final E record) {
		service.delete(record);
		return null;
	}

	@Override
	protected E editMethod(E record) {
		return service.get(record.getId());
	}

	public Ordering getOrdering() {
		return ordering;
	}

	public void setOrdering(Ordering ordering) {
		this.ordering = ordering;
	}
}
